﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using ReflectionHelper;
using Helper;

/// <summary>
/// Summary description for DDLYear
/// </summary>
/// 
namespace Web.UI.CustomControls
{
    public enum MonthDate : int
    {
        [EnumDisplayName("January","Jan")]
        JANUARY = 1,
        [EnumDisplayName("February", "Feb")]
        FEBRUARY,
        [EnumDisplayName("March", "Mar")]
        MARCH,
        [EnumDisplayName("April", "Apr")]
        APRIL,
        [EnumDisplayName("May", "May")]
        MAY,
        [EnumDisplayName("June", "Jun")]
        JUNE,
        [EnumDisplayName("July", "Jul")]
        JULY,
        [EnumDisplayName("August", "Aug")]
        AUGUST,
        [EnumDisplayName("September", "Sep")]
        SEPTEMBER,
        [EnumDisplayName("October", "Oct")]
        OCTOBER,
        [EnumDisplayName("November", "Nov")]
        NOVEMBER,
        [EnumDisplayName("December", "Dec")]
        DECEMBER
    }

    public class MonthDropDownList : DropDownList
    {
        private int selectedMonth;

        public int SelectedMonth
        {
            get
            {
                if (this.selectedMonth<0)
                {
                    try
                    {
                        this.selectedMonth = Convert.ToInt32(this.SelectedValue);
                    }
                    catch(FormatException ex)
                    {
                        throw new FormatException("The must must me an integer value.",ex);
                    }
                }

                return this.selectedMonth;
            }
            set
            {

                this.selectedMonth = value;
                this.SelectedValue = value.ToString();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!this.Page.IsPostBack)
            {

                int currentMonth = DateTime.Now.Month;
                foreach (MonthDate month in Enum.GetValues(typeof(MonthDate)))
                {
                    var monthAttribute = EnumHelper.GetAttributeOfType<EnumDisplayNameAttribute>(month);
                    this.Items.Add(new ListItem(monthAttribute.Name,Convert.ToString((int)month)));
                }
                    
                this.SelectedValue = currentMonth.ToString();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
    
}